<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>程序结构</title>
</head>
<body>
/*
鸡兔同笼问题(穷举法)
*/
//穷举法
//鸡 兔
//0 35
//1 34
//2 33
//3 32
//...
//23 12
//...
//35 0

/*
5 ! = 5 x 4 x 3 x 2 x 1
*/

/*
打印100以内除了尾数为3,5,7的所有数
*/

/*
100内的素数
*/

/*
任意自然数,累计所有位数数字之和
*/

<script>
    // 鸡兔同笼问题(穷举法)
    const totalHeads = 35;
    const totalLegs = 94;
    let solutionFound = false;
    for (let chicken = 0; chicken <= totalHeads; chicken++) {
        const rabbit = totalHeads - chicken;
        if (chicken * 2 + rabbit * 4 === totalLegs) {
            console.log('鸡: ' + chicken + '只, 兔: ' + rabbit + '只');
            solutionFound = true;
            break;
        }
    }
    if (!solutionFound) console.log('无解');
    
    // 阶乘计算
    const n = 5;
    let factorial = 1;
    for (let i = 1; i <= n; i++) {
        factorial *= i;
    }
    console.log(n + '! = ' + factorial);
    
    // 打印100以内尾数不为3,5,7的数
    console.log('100以内尾数不为3,5,7的数:');
    for (let i = 1; i <= 100; i++) {
        const lastDigit = i % 10;
        if (lastDigit !== 3 && lastDigit !== 5 && lastDigit !== 7) {
            console.log(i);
        }
    }
    
    // 100内的素数
    console.log('100内的素数:');
    for (let num = 2; num <= 100; num++) {
        let isPrime = true;
        for (let d = 2; d <= Math.sqrt(num); d++) {
            if (num % d === 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) console.log(num);
    }
    
    // 任意自然数位数和
    const inputNum = 12345;
    let sum = 0;
    let temp = inputNum;
    while (temp > 0) {
        sum += temp % 10;
        temp = Math.floor(temp / 10);
    }
    console.log(inputNum + ' 的位数和: ' + sum);
</script>
</body>
</html>